import { defineStore } from 'pinia';
import gamesApi from '@/api/modules/games';

const gamesList = defineStore({
  id: 'games',
  state: () => {
    return {
      allGames: <any>[],
      popularGames: <any>[],
      collectGames: <any>[],
      recentGames: <any>[],
    };
  },
  persist: true,
  // 等同于vuex的getter
  getters: {
    getAllGames: (state) => state.allGames,
    getPopularGames: (state) => state.popularGames,
    getCollect: (state) => state.collectGames,
    getRecentGames: (state) => state.recentGames,
  },
  actions: {
    setAllGames(list: Array<any>) {
      this.allGames = list;
    },
    setPopularGames(list: Array<any>) {
      this.popularGames = list;
    },
    setCollect()  {
      gamesApi.getUserGameCollect({}).then((response:any)=>{
        if(response.code === 200){
          console.log("最近游玩的游戲", response)
          this.collectGames = response.rows || [];
        }
      });
    },
    setRecentGames()  {
      gamesApi.getRecentGame({}).then((response:any)=>{
        if(response.code === 200){
          this.recentGames = response.data || [];
        }
      });
    },
  },

});

export default gamesList;
